﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace UserControls
{
    public partial class InfoContactUC : StudioUserControl
    {

        private Int32 _idContact = -1;
        public Int32 IdContact
        {
            get { return _idContact; }
            set
            {
                if (parentForm == null)
                    return;
                if (_idContact != value && value != -1)
                {
                    _idContact = value;
                    SqlConnection con = StudioForm.DeschideConexiune(parentForm.workConnString);
                    this.infoContactTableAdapter.Connection = con;
                    this.infoContactTableAdapter.FillByIdContact(this.eSRPDataSet.InfoContact, _idContact);
                    eSRPDataSet.InfoContact.IDContactColumn.DefaultValue = _idContact;

                    StudioForm.InchideConexiune(con);
                    if (infoContactBindingSource.Count == 0)
                        infoContactBindingSource.AddNew();
                }
            }
        }

        public InfoContactUC()
        {
            InitializeComponent();
        }

        private void InfoContactUC_Load(object sender, EventArgs e)
        {
            if (parentForm == null)
                return;
            parentForm.listBndSourceMain.Add(infoContactBindingSource);
            parentForm.listTableAdaptor.Add(infoContactTableAdapter.Adapter);
        }

        public void InitDB(StudioForm sf)
        {
            if (parentForm == null)
            {
                parentForm = sf;
            }
            if (parentForm == null)
                return;
            /*SqlConnection con = StudioForm.DeschideConexiune(parentForm.workConnString);
            this.infoContactTableAdapter.Connection = con;
            this.infoContactTableAdapter.Fill(this.eSRPDataSet.InfoContact);
            StudioForm.InchideConexiune(con);*/

        }

        private void InfoContactUC_Validating(object sender, CancelEventArgs e)
        {
            Boolean eEroare = false;
            if (telefonMTB.Text.Replace("(",String.Empty).Replace(")",String.Empty).Trim() == String.Empty)
            {
                errorProvider1.SetError(telefonMTB, "Va rugam completati telefonul persoanei de contact!");
                eEroare = true;
            }
            else
                errorProvider1.SetError(telefonMTB, String.Empty);
            
            if (eEroare)
                return;
        }

        public void SetCurrentDataSource()
        {
            if (parentForm == null)
                return;
            parentForm.SetCurrentDataSource(infoContactBindingSource);
        }

        private void InfoContactUC_VisibleChanged(object sender, EventArgs e)
        {
            if (parentForm == null)
                return;
            if (Visible)
            {
                parentForm.SetCurrentDataSource(infoContactBindingSource);
            }
        }
    }
}
